package com.BDG.Day6.repository;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;

import com.BDG.Day6.entity.Company;

@Repository
public class CompanySerImpl implements CompanySer{

	@Autowired
	private RestTemplate rest;
	@Autowired 
	private JdbcTemplate db;
	
	@Value("${myapp.userservice.url}")
	private String userServiceUrl;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<LinkedHashMap<String, String>> findAll() {
		// TODO Auto-generated method stub
		return rest.getForObject(userServiceUrl, ArrayList.class);
	}

	@Override
	public void insert(List<LinkedHashMap<String, String>> list) {
		// TODO Auto-generated method stub
		for(LinkedHashMap<String, String> map:list) {
			String date =  (String) ((Collection<String>) map.values()).toArray()[0];
			String cpName = (String) ((Collection<String>) map.values()).toArray()[1];
			String name = (String) ((Collection<String>) map.values()).toArray()[2];
			String job = (String) ((Collection<String>) map.values()).toArray()[3];
			db.update("INSERT INTO staff21732 (name,company,job,cdate) values (?,?,?,?)",name,cpName,job,date);
		}
		
	}
	
	@Override
	public void add(LinkedHashMap<String, String> map) {
		// TODO Auto-generated method stub
			String date =  (String) ((Collection<String>) map.values()).toArray()[0];
			String cpName = (String) ((Collection<String>) map.values()).toArray()[1];
			String name = (String) ((Collection<String>) map.values()).toArray()[2];
			String job = (String) ((Collection<String>) map.values()).toArray()[3];
			db.update("INSERT INTO staff21732 (name,company,job,cdate) values (?,?,?,?)",name,cpName,job,date);
		
		
	}

	@Override
	public List<Company> search(String cpName) {
		// TODO Auto-generated method stub
		return db.query("SELECT * FROM `staff21732` where company = ?",new BeanPropertyRowMapper<Company>(Company.class), cpName);
	}

	@Override
	public void update(String date,String cpName,String name,String job) {
		// TODO Auto-generated method stub
		db.update("update staff21732 set name = ?,company=?,job=?,cdate=? where company = ?",name,cpName,job,date,cpName);
		
	}

}
